from candlesimport import CandlesImport
import pandas as pd
import matplotlib
import datetime
import holoviews as hv
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings("ignore")
from holoviews.operation.timeseries import rolling, rolling_outlier_std
from holoviews.streams import Stream
hv.notebook_extension('bokeh')
def to_dataFrame(filename):
cInport = CandlesImport()
candles = cInport.importFromPickle(filename)
df = pd.DataFrame.from_dict(candles)
df['date'] = pd.to_datetime(df['date']*10**9)
df.set_index(['date'], inplace=True)
return df
candles_btc, candles_ltc = map(to_dataFrame,('data/btc360.p','data/ltc360.p'))
candles_btc.tail()
candles_ltc.tail()
def load_symbol(symbol, **kwargs):
df = data[symbol]
df['date'] = df.index
return hv.Curve(df, ('date', 'Date'), ('close', 'Close price'))
stock_symbols = ['LTC', 'BTC',]
data = {"LTC": candles_ltc, 'BTC':candles_btc}
dmap = hv.DynamicMap(load_symbol, kdims='Symbol').redim.values(Symbol=stock_symbols)
%%opts Curve [width=700] {+framewise}
dmap
%%opts Scatter [width=700] (color='black')
smoothed = dmap*rolling(dmap, rolling_window=30)*rolling_outlier_std(dmap)
smoothed
Looking correlation of Litecoin and Bitcoin
%%opts Curve [width=700, height=500, show_grid=True] {+framewise}
def rolling_corr(w):
S = candles_ltc['close'].rolling(w).corr(other=candles_btc['close'])
candles_ltc['corrBTC'] = S
return hv.Curve(candles_ltc.iloc[-2000:], ('date', 'Date'), ('corrBTC','Rolling correlation'))
winsize = {s:rolling_corr(s) for s in range(10,60,2)}
rcorr = hv.HoloMap(winsize, kdims='Window size')
rcorr
Volatility is the variance of the price. If an assert is more volatility, it is more risky.
%%opts Curve [width=700, height=500, show_grid=True] {+framewise}
def rolling_vol(w, symbol):
dt = data[symbol]
S = dt['close'].rolling(w).std()
dt['Volatility'] = S
dt['date'] = candles_ltc.index
return hv.Curve(dt.iloc[-2000:], ('date', 'Date'), ('Volatility','Rolling volatility'))
stock_symbols = ['LTC', 'BTC',]
data = {"LTC": candles_ltc, 'BTC': candles_btc}
winsize = range(10,60,2)
rvol = hv.DynamicMap(rolling_vol, kdims=['Wsize', 'Symbol']).redim.values(Symbol=stock_symbols).redim.range(Wsize=(10,60))
rvol
In this graph, we will calculate the return rate, that is $$ R = \frac{P_f}{P_p} - 1 $$ where $P_f$ is the price future and $P_p$ is the price of the past
candles_ltc['5m_return'] = candles_ltc.close/candles_ltc.close.shift(1) - 1
candles_btc['5m_return'] = candles_btc.close/candles_btc.close.shift(1) - 1
%%opts Curve [width=700, height=500, show_grid=True] {+framewise}
hv.Curve(candles_ltc[-10000:], ('date', 'Date'),('5m_return', '5m Return rate'), label='Litecoin returns').hist(num_bins=150)